from typing import Iterable

import scrapy
import json


class TestpostSpider(scrapy.Spider):
    name = "testpost"
    allowed_domains = ["fanyi.baidu.com"]
    # post 请求如果没有参数，那么这个请求将没有任何意义
    # 所以 start_urls 也没有用了
    # 所以 parse 方法也没有用了
    # start_urls = ["https://fanyi.baidu.com/sug"]
    #
    # def parse(self, response):
    #     pass

    def start_requests(self):
        url = "https://fanyi.baidu.com/sug"
        data = {
            "kw": "dog"
        }
        yield scrapy.FormRequest(
            url=url,
            formdata=data,
            callback=self.parse_second
        )

    def parse_second(self, response):
        content = json.loads(response.text)
        print(content)
